
$(document).ready(function(){
    // render
    loadData();
//    previewPicture();
    // event
    bindEvent();
});

function loadData() {

    ISS.BACKEND.request({
        url: '../../service/admin/getAllProductDetailInPicture',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllProductDetailToView(responseContent);
        }
    });
}

function renderUploadFile(maCTSP) {
    ISS.DOM.html('#upload_template', '<div id="multipleupload">Chọn đăng ảnh</div>');

    var _upload = $("#multipleupload").uploadFile({
        url: "../../service/admin/insertProductPicture",
        autoSubmit:false,
        multiple:true,
        fileName:"myfile",
        formData: {"maCTSP": maCTSP}
    });

    ISS.EVENT.bindEvent('#startUpload', 'click', function () {
        _upload.startUpload();
    });
}

function bindEvent() {
/*    ISS.EVENT.bindEvent('#ProductPicture_Add1', 'click', function () {
        uploadPicture();
    });*/

    ISS.EVENT.bindEvent('#ProductPicture_product', 'change', function () {
        renderUploadFile(ISS.DOM.getVal('#' + this.id));
    });
}

/*
function uploadPicture() {
    var number = ISS.SESSION.getSession('session_PictureNumber');
    var src;
    var product = {}, picture = {};
    product.maCTSP = ISS.DOM.getVal('#ProductPicture_product');
    picture.chiTietSanPham = product;
    picture.maHinhSP = "HA123";
    var mang =[];
    for(var i=0; i<number;i++ ){
        mang[i] = ISS.DOM.getVal('#ProductPicture_name_'+i);
        mang[i] = mang[i].replace(/^.*[\\\/]/, '');
//        alert('../../images/product/sale' + mang[i]);
        src = '\\images\\product\\sale\\' + mang[i];

        picture.tenHinh = src;

        ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/admin/insertProductPicture',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(picture),
            success: function (data) {
                var responseContent = data.responseObject;
//                loadDataComplete(responseContent);
            }
        });
    }

}
*/

/*function sendInfo() {
    var motif = {};

    motif.maHoaTiet = ISS.DOM.getVal('#AdProductMotif_code');
    motif.tenHoaTiet = ISS.DOM.getVal('#AdProductMotif_name');

    ISS.BACKEND.request({
        url: '../../service/admin/header/adProductMotif',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(motif),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}*/

function clearValidate() {
    ISS.DOM.removeClass('#AdProductMotif_code', 'error');
    ISS.DOM.removeClass('#AdProductMotif_name', 'error');
}

function validate() {
    var result = true;
    return result;
}

function mapAllProductDetailToView(productDetail) {
    var html = '<option selected="selected" value="default_value">...</option>';
    for(var i=0; i<productDetail.length; i++) {
        html += '<option value="' + productDetail[i].maCTSP + '">' + productDetail[i].maCTSP + '</option>';
    }
    ISS.DOM.html('#ProductPicture_product', html);
}